package com.fog.judge.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fog.judge.common.Base;
import com.fog.judge.common.UserContext;
import com.fog.judge.entity.Teacher;
import com.fog.judge.entity.User;
import com.fog.judge.service.QualitativeEvaluationsService;
import com.fog.judge.service.TeacherService;
import com.fog.judge.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/teacher")
public class TeacherController {
    @Resource
    private TeacherService teacherService;

    @Resource
    private UserService userService;
    @Resource
    private QualitativeEvaluationsService qualitativeEvaluationsService;

    @GetMapping("/page")
    public Base page(int page , int pageSize , String name){
        //log.info(UserContext.getUserId().toString());
        // 创建分页
        Page<User> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<User> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.orderByDesc("full_name");
        if(name !=null){
            teacherQueryWrapper.like("full_name", name);
        }
        teacherQueryWrapper.eq("role",2);
        userService.page(pageInfo, teacherQueryWrapper);
        return Base.success(pageInfo);
    }
    @GetMapping("/{id}")
    public Base getTeacherById(@PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
        User user = userService.getById(teacher.getUserId());
        return Base.success(user);
    }
    @PutMapping
    public Base changeStatusTeacher(@RequestBody Teacher teacher){
        teacherService.saveOrUpdate(teacher);
        return Base.success("更新成功");
    }
    @PostMapping
    public Base add(@RequestBody User user){
        boolean exists = userService.existsByUsername(user.getUsername());
        if(exists){
            return Base.error("用户已存在");
        }
        boolean result = userService.save(user);
        if (result) {
            return Base.success("success");
        }else {
            return Base.error("error");
        }
    }
    @DeleteMapping
    public Base deleteTeacher(String ids) {
        // 根据用户名删除用户

        userService.deleteUser(ids);

        return Base.success("用户删除成功");
    }
}
